<template>
  <div>
  <div class="plr20">
    <p class="text-555 font14">完税统计</p>
    <el-row :gutter="20" class="mb20 tongji">
      <el-col :xs="8" :sm="8" :md="12" :lg="4">
         <el-card shadow="never" align="center" style="border:0 none">
           <p class="p1">{{ statistics.batchCount }}</p>
           <p class="p2">完税账单总数</p>
         </el-card>
       </el-col>
      <el-col :xs="8" :sm="8" :md="12" :lg="4">
         <el-card shadow="never" align="center" style="border:0 none">
           <p class="p1">{{ statistics.batchCompleteCount }}</p>
           <p class="p2">已完成完税账单</p>
         </el-card>
      </el-col>
      <el-col :xs="8" :sm="8" :md="12" :lg="4">
         <el-card shadow="never" align="center" style="border:0 none">
           <p class="p1">{{ statistics.batchNoDeclareCount }}</p>
           <p class="p2">未完成完税账单</p>
         </el-card>
      </el-col>
      <el-col :xs="8" :sm="8" :md="12" :lg="4">
          <el-card shadow="never" align="center" style="border:0 none">
           <p class="p1">{{ statistics.people }}</p>
           <p class="p2">完税人数</p>
         </el-card>
      </el-col>
      <el-col :xs="8" :sm="8" :md="12" :lg="4">
         <el-card shadow="never" align="center" style="border:0 none">
           <p class="p1">{{ statistics.declareCompleteCount }}</p>
           <p class="p2">完税次数</p>
         </el-card>
      </el-col>
      <el-col :xs="8" :sm="8" :md="12" :lg="4">
         <el-card shadow="never" align="center" style="border:0 none">
           <p class="p1">{{ statistics.declareCompleteMoney }}</p>
           <p class="p2">完税总额</p>
         </el-card>
      </el-col>
    </el-row>
  </div>
  <div class="app-container">  
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="完税省份" prop="provinceCode">
            <el-select v-model="queryParams.provinceCode" placeholder="全部" clearable filterable>
              <el-option
                v-for="dict in provinceOptions"
                :key="dict.provinceCode"
                :label="dict.provinceFullName"
                :value="dict.provinceCode"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="管理企业" prop="companyAdminId">
            <el-select v-model="queryParams.companyAdminId" value-key="companyId" placeholder="请选择" clearable filterable @change="change">
              <el-option
                v-for="dict in enterpriseDropDownOptions"
                :key="dict.companyId"
                :label="dict.companyName"
                :value="dict.companyId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="客户企业" prop="companyId" v-if="isShow">
            <el-select v-model="queryParams.companyId" placeholder="请选择" clearable  filterable>
              <el-option
                v-for="dict in clientCompany"
                :key="dict.companyId"
                :label="dict.companyName"
                :value="dict.companyId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="申报状态" prop="declareStatus">
            <el-select
              v-model="queryParams.declareStatus"
              placeholder="全部"
              clearable
              style="width: 150px"
            >
              <el-option label="未申报" value="0"></el-option>
              <el-option label="部分申报" value="2"></el-option>
              <el-option label="已申报" value="3"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="startDate">
              <el-date-picker
              v-model="queryParams.startDate"
              style="width: 140px"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="date"
              placeholder="开始日期">
              </el-date-picker>
          </el-form-item>
          <el-form-item>-</el-form-item>
          <el-form-item prop="endDate">
              <el-date-picker
              v-model="queryParams.endDate"
              style="width: 140px"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="date"
              placeholder="结束日期">
              </el-date-picker>
          </el-form-item>


          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="medium" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="medium" @click="resetQuery">重置</el-button>
          </el-form-item>
          <el-form-item>
            <el-button
              type="warning"
              plain
              size="medium"
              icon="el-icon-download"
              @click="handleBatchExport"
              v-hasPermi="['taxmsg:declare:batchDownload']"
            >导出账单</el-button>
          </el-form-item>
        </el-form>

        <!-- <el-row :gutter="10" class="mb10">
          <el-col :span="1.5">
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> 
        </el-row> -->

        <el-table v-loading="loading" :data="listDeclare">
          <!-- <el-table-column type="selection" width="50" align="center" /> -->
          <el-table-column fixed label="序号" prop="" width="55" align="center">
            <template slot-scope="scope">
              <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
            </template>
          </el-table-column>
          <el-table-column label="管理企业" align="left" key="companyAdminNames" prop="companyAdminNames" :show-overflow-tooltip="true"/>
          <el-table-column label="实际管理企业" align="left" key="actualCompanyAdminNames" prop="actualCompanyAdminNames" :show-overflow-tooltip="true"/>
          <el-table-column label="客户企业" align="left" key="companyNames" prop="companyNames" :show-overflow-tooltip="true"/>
          <el-table-column label="实际客户企业" align="left" key="actualCompanyNames" prop="actualCompanyNames" :show-overflow-tooltip="true"/>
          <el-table-column label="完税省份" align="left" key="provinceName" prop="provinceName" />
          <el-table-column label="申报日期" align="left" key="months" prop="months" :show-overflow-tooltip="true"/>
          <el-table-column label="实际申报日期" align="left" key="actualMonths" prop="actualMonths" :show-overflow-tooltip="true"/>
          <el-table-column label="总人数" align="left" key="totalPeople" prop="totalPeople" />
          <el-table-column label="总金额" align="left" key="totalMoney" prop="totalMoney" />
          <el-table-column label="完税状态" align="left" key="declareStatus.name" prop="declareStatus.name" />
          <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" width="150">
            <template slot-scope="scope">
              <el-dropdown size="mini" split-button type="primary" @click="handleView(scope.row)" placement="bottom-start" trigger="click">
                详 情
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item icon="el-icon-download" @click.native="handleExport(scope.row)" v-hasPermi="['taxmsg:declare:dataDownload']">导出数据</el-dropdown-item>
                  <el-dropdown-item icon="el-icon-upload2" @click.native="handleImport(scope.row)" v-hasPermi="['taxmsg:declare:importUpdateData']">导入数据</el-dropdown-item>
                  <el-dropdown-item icon="el-icon-caret-right" @click.native="handleTaxpayment(scope.row)" v-hasPermi="['taxmsg:declare:update']">确认完税</el-dropdown-item>
                  <el-dropdown-item icon="el-icon-upload2" @click.native="handleUpload(scope.row)" v-hasPermi="['taxmsg:declare:fileBatchUpload']">上传凭证</el-dropdown-item>
                  <el-dropdown-item icon="el-icon-upload2" @click.native="handleCompanyView(scope.row)" v-hasPermi="['taxmsg:declare:companyFileUpload']">客户企业凭证</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
              <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
            >详情</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-download"
              @click="handleExport(scope.row)"
            >导出数据</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-upload2"
              @click="handleImport(scope.row)"
            >导入数据</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-caret-right"
              @click="handleTaxpayment(scope.row)"
            >确认完税</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-upload2"
              @click="handleUpload(scope.row)"
            >上传凭证</el-button> -->
            <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-download"
              @click="(scope.row)"
            >下载凭证</el-button> -->
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- Excel导入修改申报数据对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 上传凭证对话框 -->
    <el-dialog title="上传凭证" :visible.sync="openProof" width="400px" append-to-body>
      <el-upload
      ref="upload1"
      action=""
      :limit=limitNum
      :auto-upload="false"
      accept="*"
      :before-upload="beforeUploadFile"
      multiple
      :on-exceed="exceedFile"
      :on-success="handleSuccess"
      :file-list="fileList"
      drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <!-- <div class="el-upload__tip" slot="tip">
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div> -->
        <div class="el-upload__tip" style="color:red" slot="tip">
              <p>提示：文件名称根据一定规则匹配系统数据，完成上传逻辑！</p>
              <p>匹配规则：</p>
              <p>● 申报数据id+后缀名称 如:140.jpg、140.pdf、140.docx</p>
              <p>● 申报数据id+'_'+备注信息 +后缀名称 如：140_备注测试.jpg、140_第二份文件.jpg 、140_第一份文件.pdf</p>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm1">确 定</el-button>
        <el-button @click="cancelProof">取 消</el-button>
      </div>
    </el-dialog>

  </div>
  </div>
</template>

<script>
import { declareList, declareStatistics, declareDataDownload, declareBatchDownload, declareUpdate, declareFileBatchUpload } from "@/api/taxmsg/declare";
import { getToken } from "@/utils/auth";
export default {
  name: "Declare", 
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 用户表格数据
      listDeclare: null,
      // 申报完税统计数据统计
      statistics: [],
      // 省份信息数据字典
      provinceOptions: [],
      // 所有的管理企业和关联客户企业信息数据字典
      enterpriseDropDownOptions: [],
      clientCompany:[],
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（凭证）
      openProof: false,
      isShow:false,
      // Excel导入修改申报数据参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization:getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/tax/declare/import/update/data"
      },
      limitNum: 5,
      fileList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        provinceCode: undefined,
        companyAdminId: undefined,
        companyId: undefined,
        declareStatus: undefined,
        startDate: undefined,
        endDate: undefined,
      }
     
    };
  },
  created() {
    this.getList();
    this.getStatistics();
    this.getProvince().then(response => {
      this.provinceOptions = response.data;
    });
    this.getEnterpriseDropDown().then(response => {
      this.enterpriseDropDownOptions = response.data;
    });
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      if(this.queryParams.declareStatus==""){
        this.queryParams.declareStatus = null
      }
      declareList(this.queryParams).then(response => {
          this.listDeclare = response.data.list;
          this.total = response.data.total;
          this.loading = false;
        }).catch(()=>{});
    },
    change(e){
      delete this.queryParams.companyId
      if(e){
        this.isShow = true;
        this.clientCompany = this.enterpriseDropDownOptions.filter(item => item.companyId === e)[0].clientCompany;
      }else{
        this.isShow = false
      }
    },
    getStatistics(){
      this.loading = true;
      declareStatistics().then(response => {
          this.statistics = response.data;
          this.loading = false;
        }
      );
    },
    /** 修改按钮操作 */
    handleView(row) {
      const id = row.id || this.ids[0];
      this.$router.push("/declare/details/" + id);
    },
     /** 客户企业完税凭证 **/
    handleCompanyView(row){
      const id = row.id || this.ids[0];
      this.$router.push("/declare/companyFileList/" + id);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      //this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
      this.isShow = false;
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 导出按钮操作 */
    handleExport(row) {
      const batchId = row.id || this.ids;
      this.$confirm('是否确认导出编号为"' + batchId + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return declareDataDownload({batchId});
        }).then(response => {
          //this.download(response.msg);
          var list_name = "申报数据";
          this.export_excel_file(response, list_name);
        }).catch(()=>{})
    },
    handleBatchExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          if(queryParams.declareStatus==""){
            queryParams.declareStatus = null
          }
          return declareBatchDownload(queryParams);
        }).then(response => {
          //this.download(response.msg);
          var list_name = "批量申报数据";
          this.export_excel_file(response, list_name);
        }).catch(()=>{})
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "导入修改申报数据";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      // importTemplate().then(response => {
      //   this.download(response.msg);
      // });
      window.location.href = process.env.VUE_APP_BASE_API + "/tax/public/excel/taxDeclareImportTpl.xlsx";
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      //alert(response.code);
      this.$alert(response.data.message, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /*************************** 上传凭证 **********************************/
    cancelProof() {
      this.openProof = false;
    },
    handleUpload(row) {
      this.openProof = true;
    },
    exceedFile(files, fileList) {
      this.$notify.warning({
        title: '警告',
        message: `只能选择 ${this.limitNum} 个文件，当前共选择了 ${files.length + fileList.length} 个`
      });
    },
    // 上传文件之前的钩子, 参数为上传的文件,若返回 false 或者返回 Promise 且被 reject，则停止上传
    beforeUploadFile(file) {
      //console.log(file)
      const size = file.size / 1024 / 1024
      if(size > 200) {
        this.$notify.warning({
          title: '警告',
          message: `文件大小不得超过200M`
        });
      }
      const fd = new FormData()
      fd.append('files', file)
      //备注
      //fd.append('groupId', this.groupId)
      declareFileBatchUpload(fd).then(response => {
          this.openProof = false;
          this.$alert(response.data.message, "导入结果", { dangerouslyUseHTMLString: true });
          this.getList();
      });
      return true
    },
    // 文件上传成功时的钩子
    handleSuccess(files, fileList) {
      this.$notify.success({
        title: '成功',
        message: `文件上传成功`
      });
    },
    submitFileForm1() {
      this.$refs.upload1.submit()
    },
    // 确认完税
    handleTaxpayment(row) {
      //const id = row.id || this.ids;
      this.$confirm('是否确认完税？批量确认完税只修改未申报状态的数据为已申报', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return declareUpdate(row.id,3);
        }).then(response => {
          this.getList();
          this.msgSuccess(response.msg);
        }).catch(()=>{})
    },
  }
};
</script>
<style scoped lang="scss">
.tongji {
  p.p1{
    font-size: 18px;
    color: #045aff;
    font-weight: bold;
  }
  p.p2{
    font-size: 13px;
    color: #676a6c;
  }
}
</style>